package com.wiscamp.ninechapters.problems.infrastructure.mappers.mistakes;

import com.wiscamp.ninechapters.problems.domain.models.mistakes.MistakeQuantityBySetType;
import com.wiscamp.ninechapters.problems.infrastructure.po.mistakes.MistakeView;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.Collection;

@Repository
public interface MistakeViewMapper extends JpaRepository<MistakeView, Long>, JpaSpecificationExecutor<MistakeView> {

    /**
     * 分页获取错题本
     *
     * @param spec
     * @param pageable
     * @return
     */
    Page<MistakeView> findAll(Specification<MistakeView> spec, Pageable pageable);

    @Query(value = "select count(e.mistakeId) from MistakeView e where e.userId = :userId")
    int countMistakesQuantityByUser(long userId);

    @Query(value = "SELECT new com.wiscamp.ninechapters.problems.domain.models.mistakes.MistakeQuantityBySetType(e.setTypeId, count(e.mistakeId)) FROM MistakeView e where e.userId = :userId group by e.setTypeId")
    Collection<MistakeQuantityBySetType> findGroupedMistakeQuantity(long userId);

    @Query(value = "SELECT e FROM MistakeView e where e.userId = :userId and e.setTypeId = :setTypeId")
    Collection<MistakeView> findMistakesBySetType(long userId, long setTypeId);

    @Query(value = "SELECT e FROM MistakeView e where e.userId = :userId")
    Collection<MistakeView> findMistakesByUser(long userId);

    @Query(value = "select e from MistakeView e where e.userId = :userId and e.taskId = :taskId and e.setId = :setId and e.problemId = :problemId and e.questionId = :questionId")
    MistakeView findMistake(long userId, long taskId, long setId, long problemId, long questionId);

    /*Page<MistakeView> findAll(Specification<MistakeView> spec, Pageable pageable);

    @Query(value = "SELECT e.setTypeId as setType, count (e.id) as mistakeQuantity from MistakeView e where e.userId = :userId group by e.setTypeId")
    Collection<Tuple> findGroupedMistakesQuantityBySetType(long userId);

    @Query(value = "SELECT e from MistakeView e where e.userId = :userId and e.setTypeId = :setType")
    Collection<MistakeView> findMistakesBySetType(long userId, long setType);*/
}
